package cn.house.controller.admin;

import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.house.common.EntityConstants;
import cn.house.common.LayuiTableResult;
import cn.house.common.Pagination;
import cn.house.common.PublicResultJosn;
import cn.house.controller.BaseController;
import cn.house.entity.EnteringInfo;
import cn.house.service.EnteringInfoService;

/**
 * <p>
 * 入驻信息表 前端控制器
 * </p>
 *
 * @author yuyong
 * @since 2019-02-21
 */
@RestController
@RequestMapping("/entering-info")
public class EnteringInfoController extends BaseController {

	@Autowired
	private EnteringInfoService enteringInfoService;

	/**
	 * 删除（回收）不入驻房源信息，不入驻，过时未入驻
	 */
	public PublicResultJosn delete(@RequestBody List<Integer> ids) {
		if (CollectionUtils.isEmpty(ids)) {
			throw new IllegalArgumentException("不合法参数");
		}
		List<EnteringInfo> entityList = ids.parallelStream().map(id -> {
			EnteringInfo enteringInfo = new EnteringInfo();
			enteringInfo.setId(id);
			enteringInfo.setStatus(EntityConstants.DATA_NO);
			return enteringInfo;
		}).collect(Collectors.toList());
		enteringInfoService.updateBatchById(entityList);
		return new PublicResultJosn();
	}

	/**
	 * 确认入驻
	 */
	public PublicResultJosn updateById(@RequestBody List<Integer> ids) {
		if (CollectionUtils.isEmpty(ids)) {
			throw new IllegalArgumentException("不合法参数");
		}
		List<EnteringInfo> entityList = ids.parallelStream().map(id -> {
			EnteringInfo enteringInfo = new EnteringInfo();
			enteringInfo.setId(id);
			enteringInfo.setStatus(EntityConstants.DATA_YES);
			return enteringInfo;
		}).collect(Collectors.toList());
		enteringInfoService.updateBatchById(entityList);
		return new PublicResultJosn();
	}

	/**
	 * 分页模糊查询
	 */
	public LayuiTableResult selectByPage(Pagination pagination, EnteringInfo enteringInfo) {
		Wrapper<EnteringInfo> queryWrapper = new QueryWrapper<>(enteringInfo);
		IPage<EnteringInfo> page = new Page<>(pagination.getPage(), pagination.getPageSize());
		IPage<EnteringInfo> pageResult = enteringInfoService.page(page, queryWrapper);
		LayuiTableResult result = new LayuiTableResult(pageResult.getTotal(), pageResult.getRecords());
		return result;
	}
}
